(library (handlers static-asset)
  (export static-asset-handler)
  (import
    (except (rnrs base) let-values error)
    (only (guile) lambda* λ)

    (prefix (logging) log:)

    (lib utils response-utils)
    (lib utils request-utils)

    (lib web-location-handling)
    (prefix (fslib) fslib:)

    (handlers not-found-404)))

;;; handlers/static-asset.scm:36:10: warning: possibly unbound variable `not-found-404-handler'


(define static-asset-handler
  (lambda (request request-body)
    "A handler for static assets."

    (define req-path-components (request-path-components request))
    (define not-null-request-path
      (if (null? req-path-components)
          '("/")
          req-path-components))

    (let ([fs-loc (apply fslib:fsing-join not-null-request-path)])
      (log:debug "file system location is:" fs-loc)
      (cond
       [(static-asset-location? fs-loc)
        (log:debug "request path is a static asset path:" fs-loc)
        ;; Check, whether the static asset route is OK to access. If static
        ;; asset is OK to access, then serve it.
        (cond
         ;; All security hinges on safe/static-asset-location?, so it better be
         ;; secure!
         [(safe/static-asset-location? fs-loc)
          (create-static-asset-response fs-loc)]
         ;; If the path is not safe, refuse, by answering with a 404 HTTP status
         ;; code.
         [else
          (log:debug "using 404 handler for" req-path-components)
          (not-found-404-handler request request-body)])]
       [else
        (log:debug "using 404 handler for" req-path-components)
        (not-found-404-handler request request-body)]))))
